import sys
from classRules import cppProcess
from readFile import ReadFile
from writeFile import WriteFile


def sortInst(file_path):
    listcontents = ReadFile.readFile(file_path)
    dicstatus = {}
    dicexception = {}
    for contents in listcontents:
        inst = cppProcess(contents)
        key = str(inst.basis_so)
        value = inst
        if key in dicstatus:
            dicstatus[key].append(value)
        else:
            dicstatus[key] = [value]
    dic_total = {**dicstatus, **dicexception}
    return dic_total


def main(file_path):
    sortedinst = sortInst(file_path)
    WriteFile.write(file_path, sortedinst)


if __name__ == '__main__':
    argv = sys.argv
    version = 'VERSION: 2023_07_06'
    if len(argv) != 2:  # 判断元素个数
        print("usage: python main.py cppcrash_files_dir")
    else:
        if argv[1] == '-v':
            print(version)
        else:
            print(version)
            main(argv[1])
